package com.hnkywl.sts.dao.jsgl;

import com.hnkywl.sts.dao.base.RegionDao;
import com.hnkywl.sts.entity.cggl.Jsd;
import com.hnkywl.sts.entity.xsgl.Xsmx;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.Criteria;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-4-23
 * Time: 上午10:54
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class XsjsdDao extends SimpleHibernateTemplate<Jsd, Long> {
    public Page<Jsd> findXsjsdList(Integer zt, String jsdh, Date sqrqStart, Date sqrqEnd, Long glqyId, String glqyQc, Long gsId, String gsJc, Long fpzlId, int pageNo, int pageSize) {
        String hql;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Jsd> page = new Page<Jsd>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (jsdh != null && !jsdh.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" jsdh like ? ");
            param.add("%" + jsdh.trim() + "%");
        }
        if (sqrqStart != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" sqrq >= ?");
            param.add(sqrqStart);
        }
        if (sqrqEnd != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" sqrq <= ?");
            param.add(sqrqEnd);
        }
        if (glqyId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" glqy.id = ? ");
            param.add(glqyId);
        }
        if (zt != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" zt = ? ");
            param.add(zt);
        }
        if (glqyQc != null && !"".equals(glqyQc)) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" glqy.qc like ? ");
            param.add("%" + glqyQc.trim() + "%");
        }
        if (gsId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" gs.id = ? ");
            param.add(gsId);
        }
        if (gsJc != null && !"".equals(gsJc)) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" gs.jc like ? ");
            param.add("%" + gsJc.trim() + "%");
        }
        if (fpzlId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" fpzl = ? ");
            param.add(fpzlId);
        }
        if (condition.length() != 0)
            condition.append(" and ");
        condition.append(" type = ? ");
        param.add(1);
        //==============记录数量===============
        hql = "select count(*) from Jsd where sftzd!=1 and deleted!=1 and zt in (0,1,2)" + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Jsd where sftzd!=1 and deleted!=1 and zt in (0,1,2)" + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition + "order by dateCreated desc";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }

    public String findAllXsjsd(Integer zt, Integer type) {
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();

        if (zt == 0) {
            condition.append(" (zt = '0' or zt='1') ");
        } else {
            condition.append(" zt = ? ");
            param.add(zt);
        }
        condition.append(" and type = ? ");
        param.add(type);
        String hql = "select count(*) from Jsd where deleted!=1  " + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        Long c = (Long) findUnique(hql, param.toArray());
        return c.toString();
    }

    public List<Map> xsjsdList(Long xsjsdId) {
        Criteria criteria = getSession().createCriteria(Xsmx.class)
                .createAlias("kh", "kh", CriteriaSpecification.LEFT_JOIN)
                .createAlias("jtwz", "wzgg", CriteriaSpecification.LEFT_JOIN)
                .createAlias("kh.cd", "cd", CriteriaSpecification.LEFT_JOIN)
                .createAlias("jzfs", "jzfs", CriteriaSpecification.LEFT_JOIN)
                .createAlias("xsd", "xsd", CriteriaSpecification.LEFT_JOIN)
                .createAlias("xsjsd", "xsjsd", CriteriaSpecification.LEFT_JOIN)
                .setProjection(Projections.projectionList()
                                .add(Property.forName("id").as("id"))
                                .add(Property.forName("xsd.id").as("xsd.id"))
                                .add(Property.forName("xsd.xsdh").as("xsdh"))
                                .add(Property.forName("kh.id").as("kh.id"))
                                .add(Property.forName("xscb").as("xscb"))
                                .add(Property.forName("wzgg.id").as("wzgg.id"))
                                .add(Property.forName("wzgg.code").as("wzgg.code"))
                                .add(Property.forName("wzgg.name").as("wzgg.name"))
                                .add(Property.forName("wzgg.cz").as("wzgg.cz"))
                                .add(Property.forName("wzgg.ggxh").as("wzgg.ggxh"))
                                .add(Property.forName("wzgg.dw").as("wzgg.dw"))
                                .add(Property.forName("xsxt_xsdh").as("xsxt_xsdh"))
                                .add(Property.forName("js").as("js"))
                                .add(Property.forName("sbsl").as("sbsl"))
                                .add(Property.forName("jzfs.id").as("jzfs.id"))
                                .add(Property.forName("jzfs.z").as("jzfs.z"))
                                .add(Property.forName("ck_js").as("ck_js"))
                                .add(Property.forName("cd.id").as("cd.id"))
                                .add(Property.forName("cd.z").as("cd.z"))
                                .add(Property.forName("ck_sbsl").as("ck_sbsl"))
                                .add(Property.forName("hsj").as("hsj"))
                                .add(Property.forName("sjdj").as("sjdj"))
                                .add(Property.forName("kpbhsj").as("kpbhsj"))
                                .add(Property.forName("bhsj").as("bhsj"))
                                .add(Property.forName("jsbhse").as("je"))
                                .add(Property.forName("se").as("fpse"))
                                .add(Property.forName("jsse").as("se"))
                                .add(Property.forName("xsjsd.ce").as("ce"))
                                .add(Property.forName("xsjsd.ddfy").as("ddfy"))
                ).add(Restrictions.eq("xsjsd.id", xsjsdId))
                .setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
        return (List<Map>) criteria.list();
    }
}
